//
//  ECCommodityMenuView.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/10/26.
//  Copyright © 2018年 Mac. All rights reserved.
//

#import "ECCommodityMenuView.h"

@interface ECCommodityMenuView()<UITableViewDelegate>

@property (nonatomic,strong) NSMutableDictionary<NSString*,UITableView*> * commodityListTableViewsDic;

@end

@implementation ECCommodityMenuView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        self.commodityListTableViewsDic = @{}.mutableCopy;
    }
    return self;
}

- (void)setupUI {
    __weak typeof(self) weakSelf = self;
    [self addSubview:self.menuTableView];
    [self.menuTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(weakSelf);
        make.width.mas_equalTo(64 * PX_RELATIVE375);
    }];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.menuTableView) {
        if (![[self.commodityListTableViewsDic allKeys] containsObject:[NSString stringWithFormat:@"%ld",indexPath.row]]) {
            UITableView * newTableView = [self createListTableView];
            [self addSubview:newTableView];
            __weak typeof(self) weakSelf = self;
            [newTableView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.right.bottom.mas_equalTo(weakSelf);
                make.left.mas_equalTo(weakSelf).mas_offset(64 * PX_RELATIVE375);
            }];
            [self.commodityListTableViewsDic setObject:newTableView forKey:[NSString stringWithFormat:@"%ld",indexPath.row]];
        }
        if (tableView == self.currentCommodityListTableView) {
            return;
        }
        if (self.currentCommodityListTableView) {
            self.currentCommodityListTableView.hidden = YES;
        }
        self.currentCommodityListTableView = [self.commodityListTableViewsDic objectForKey:[NSString stringWithFormat:@"%ld",indexPath.row]];
        self.currentCommodityListTableView.hidden = NO;
        self.currentCommodityListTableView.dataSource = self.commodityListTableViewDelegate;
        self.currentCommodityListTableView.delegate = self.commodityListTableViewDelegate;
        if (self.changeSelectedMenuItmeBlock) {
            self.changeSelectedMenuItmeBlock(indexPath.row);
        }
    }
}

- (UITableView*)createListTableView {
    UITableView * tableView = [UITableView new];
    tableView.bounces = NO;
    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00001)];
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00001)];
    return tableView;
}

- (UITableView *)menuTableView {
    if (!_menuTableView) {
        _menuTableView = [[UITableView alloc] init];
        _menuTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00001)];
        _menuTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00001)];
        _menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _menuTableView.bounces = NO;
        _menuTableView.delegate = self;
    }
    return _menuTableView;
}

- (void)setMenuTableViewDataSource:(id<UITableViewDataSource>)menuTableViewDataSource {
    _menuTableViewDataSource = menuTableViewDataSource;
    self.menuTableView.dataSource = _menuTableViewDataSource;
}

@end

@interface ECCommodityMenuTableViewCell()

@property (nonatomic,strong) CAShapeLayer * bottomLine;

@end

@implementation ECCommodityMenuTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setupUI {
    __weak typeof(self) weakSelf = self;
    self.titlLB = [UILabel new];
    self.titlLB.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.titlLB];
    [self.titlLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.contentView);
        make.left.mas_equalTo(weakSelf.contentView).mas_offset(12 * PX_RELATIVE375);
        make.right.mas_equalTo(weakSelf.contentView);
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.bottomLine.frame = CGRectMake(12 * PX_RELATIVE375, self.contentView.height-2*PX_RELATIVE375, self.contentView.width - 12 * PX_RELATIVE375, 2 * PX_RELATIVE375);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if (selected) {
        self.titlLB.textColor = ECCOLOR(0x333333);
        self.titlLB.font = ECBOLDFONT(14 * PX_RELATIVE375);
        self.bottomLine.hidden = NO;
    }else {
        self.titlLB.textColor = ECCOLOR(0x999999);
        self.titlLB.font = ECFONT(14 * PX_RELATIVE375);
        self.bottomLine.hidden = YES;
    }
}

- (CAShapeLayer *)bottomLine {
    if (!_bottomLine) {
        _bottomLine = [CAShapeLayer layer];
        _bottomLine.borderWidth = 1 * PX_RELATIVE375;
        _bottomLine.borderColor = ECCOLOR(0x41d95a).CGColor;
        [self.contentView.layer addSublayer:self.bottomLine];
        _bottomLine.hidden = YES;
    }
    return _bottomLine;
}

@end
